package com.loyal.loyalerp.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.junit.Test;

public class WorkDayMath {
 
    //导入一年信息
    public static int[] importDate(String years) throws ParseException {
    	int workDays[] = new int[12]; 
    	SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    	SimpleDateFormat format1 = new SimpleDateFormat("yyyy年MM月dd日EE",Locale.CHINA);
    	Date start = format.parse(years+"0101");//开始时间
    	Date end = format.parse(years+"1231");//结束时间
    	List<Date> li = splitDate(start, end);
    	int count1 = 0;
    	int count2 = 0;
    	int count3 = 0;
    	int count4 = 0;
    	int count5 = 0;
    	int count6 = 0;
    	int count7 = 0;
    	int count8 = 0;
    	int count9 = 0;
    	int count10 = 0;
    	int count11 = 0;
    	int count12 = 0;
    	for(Date date:li) {
    		String newDate = format1.format(date);
    		String yearNum = newDate.substring(0, newDate.indexOf("年"));
    		String month = newDate.substring(newDate.indexOf("年")+1,newDate.indexOf("月"));
    		String weekNum = newDate.substring(newDate.indexOf("日")+1,newDate.length());

    		if(date.getMonth()==0) {
    			
    			if((date.getDay()!=0)&&(date.getDay()!=6)) {
    				count1++;
    			}
    			
    		}
    		if(date.getMonth()==1) {
    			
    			if((date.getDay()!=0)&&(date.getDay()!=6)) {
    				count2++;
    			}
    			
    		}

    		if(date.getMonth()==2) {
    			
    			if((date.getDay()!=0)&&(date.getDay()!=6)) {
    				count3++;
    			}
    			
    		}

    		if(date.getMonth()==3) {
    			
    			if((date.getDay()!=0)&&(date.getDay()!=6)) {
    				count4++;
    			}
    			
    		}
    		if(date.getMonth()==4) {
    			
    			if((date.getDay()!=0)&&(date.getDay()!=6)) {
    				count5++;
    			}
    			
    		}
    		if(date.getMonth()==5) {
    			
    			if((date.getDay()!=0)&&(date.getDay()!=6)) {
    				count6++;
    			}
    			
    		}
    		if(date.getMonth()==6) {
    			
    			if((date.getDay()!=0)&&(date.getDay()!=6)) {
    				count7++;
    			}
    			
    		}
    		if(date.getMonth()==7) {
    			
    			if((date.getDay()!=0)&&(date.getDay()!=6)) {
    				count8++;
    			}
    			
    		}
    		if(date.getMonth()==8) {
    			
    			if((date.getDay()!=0)&&(date.getDay()!=6)) {
    				count9++;
    			}
    			
    		}

    		if(date.getMonth()==9) {
    			
    			if((date.getDay()!=0)&&(date.getDay()!=6)) {
    				count10++;
    			}
    			
    		}

    		if(date.getMonth()==10) {
    			
    			if((date.getDay()!=0)&&(date.getDay()!=6)) {
    				count11++;
    			}
    			
    		}

    		if(date.getMonth()==11) {
    			
    			if((date.getDay()!=0)&&(date.getDay()!=6)) {
    				count12++;
    			}
    			
    		}
    	}
        workDays[0] = count1;
        workDays[1] = count2;
        workDays[2] = count3;
        workDays[3] = count4;
        workDays[4] = count5;
        workDays[5] = count6;
        workDays[6] = count7;
        workDays[7] = count8;
        workDays[8] = count9;
        workDays[9] = count10;
        workDays[10] = count11;
        workDays[11] = count12;
		return workDays;
    }
    
    //日期分隔
    public static List<Date> splitDate(Date start,Date end){
    	Long spi = end.getTime() - start.getTime();
    	//计算相隔的天数
    	Long num = spi/(1000*60*60*24);
    	List<Date> list = new ArrayList<Date>();
    	list.add(start);
    	for(int i=1;i<=num;i++) {
    		list.add(new Date(list.get(i-1).getTime()+(1000*60*60*24)));
    	}
    	return list;
    }
    @Test
    public void testday() throws ParseException {
    	System.out.println(mathYearDays(new Date(), "2018"));
    }
    
   //计算入职的时间离年底还剩多少天
    public static float mathYearDays(Date date,String years) throws ParseException {
    	 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    	 Date yearDays = format.parse(years+"1231");
    	 long days =  (yearDays.getTime()-date.getTime())/(1000*60*60*24); 
    	 if(Integer.parseInt(years)%4==0) {
    		 return days/366.0f;
    	 }
    	 else {
    		 return days/365.0f;
    	 }
    }
  
}
